package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.entity.Topic;
import com.service.TopicService;
import com.service.impl.TopicServiceImpl;

/**
 * Servlet implementation class TopicServlet
 */
@WebServlet("/topic")
public class TopicServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TopicServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Map<String, Object> resultMap = new HashMap<String, Object>(4);
		PrintWriter out = response.getWriter();
		try {
			request.setCharacterEncoding("utf-8");
			String opt = request.getParameter("opt");
			TopicService topicService = new TopicServiceImpl();
			boolean flag = true;
			
			if ("del".equals(opt)) {
				//删除
				
				String tid = request.getParameter("tid");
				flag = topicService.deleteTopic(Integer.parseInt(tid));
				resultMap.put("flag", flag);
				resultMap.put("message", flag?"删除成功!":"删除失败");
				out.print(JSON.toJSONString(resultMap));
			} else if ("add".equals(opt)) {
				//添加主题
				String tname = request.getParameter("tname");
				flag = topicService.addTopic(tname);
				resultMap.put("flag", flag);
				resultMap.put("message", flag?"添加成功!":"添加失败");
				out.print(JSON.toJSONString(resultMap));
			} else if ("upd".equals(opt)) {
				//修改主题
				String tname = request.getParameter("tname");
				String tid = request.getParameter("tid");
				
				flag = topicService.updateTopic(Integer.parseInt(tid), tname);
				resultMap.put("flag", flag);
				resultMap.put("message", flag?"修改成功!":"修改失败");
				out.print(JSON.toJSONString(resultMap));
			} else if("get".equals(opt)) {
				//获取一个主题并转发到修改主题页面
				String tid = request.getParameter("tid");
				Topic topic = topicService.getTopic(Integer.parseInt(tid));

				out.print(JSON.toJSONString(topic));
			} else{
				List<Topic> topics = topicService.selectTopics();
				request.setAttribute("topics", topics);
				request.getRequestDispatcher("admin/topic_list.jsp").forward(request, response);
			}
			
			out.flush();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
